package com.open.exam.mapper;

import com.open.exam.databasemodel.ExamScenes;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author 奶茶
 * Created on 2017/11/14.
 */
@Mapper
public interface ExamScenesMapper {

    @Select("SELECT * FROM ExamScenes WHERE ExamSceneID = #{examSceneID}")
    ExamScenes examScenedFirstOrDefault(@Param(value = "examSceneID") String examSceneID);

    @Select("SELECT * FROM ExamScenes WHERE AppID = #{appID} AND ExamPlanInfoID = #{examPlanInfoID} AND ExamSceneID = #{examSceneID} LIMIT 1")
    ExamScenes getExamScenesBySomeWhere(@Param(value = "appID") String appID, @Param(value = "examPlanInfoID") String examPlanInfoID, @Param(value = "examSceneID") String examSceneID);

    //@SelectProvider(type = ExamScenesProvider.class,method = "planId")
    @Select("SELECT * FROM examscenes WHERE ExamPlanInfoID = #{planId}")
    List<ExamScenes> findExamScenesListBySomeWhere(@Param(value = "appId") String appId, @Param(value = "planId") String planId, @Param(value = "roomId") String roomId, @Param(value = "subjectId") String subjectId);

    @Insert("INSERT INTO ExamScenes (\n" +
            "\tAppID,ExamPlanInfoID,ExamSceneID,ExamSceneCode,ExamSceneName,PlanBeginTime,PlanEndTime,SysState\n" +
            ") SELECT #{appID},#{examPlanInfoID},#{examSceneID},#{examSceneCode},#{examSceneName},#{planBeginTime},#{planEndTime},#{sysState}\n" +
            "FROM\n" +
            "\tDUAL\n" +
            "WHERE\n" +
            "\tNOT EXISTS (\n" +
            "\t\tSELECT\n" +
            "\t\t\t1\n" +
            "\t\tFROM\n" +
            "\t\t\tExamScenes\n" +
            "\t\tWHERE\n" +
            "\t\t\tExamPlanInfoID = #{examPlanInfoID}\n" +
            "\t\tAND ExamSceneID = #{examSceneID}\n" +
            "\t\tLIMIT 1\n" +
            ")")
    Integer insertExamScenes(ExamScenes examScenes);

    @Update("UPDATE ExamScenes SET ExamSceneCode = #{examSceneCode},ExamSceneName = #{examSceneName},PlanBeginTime = #{planBeginTime},SysState = #{sysState} WHERE AppID = #{appID} AND ExamPlanInfoID = #{examPlanInfoID} AND ExamSceneID = #{examSceneID}")
    Integer updateExamScenes(ExamScenes examScenes);

    @Delete("DELETE es FROM ExamScenes es \n" +
            "INNER JOIN ExamineeArrange ea \n" +
            "ON es.ExamSceneID = ea.ExamSceneID \n" +
            "WHERE ea.AppID = #{appID} AND ea.ExamPlanInfoID = #{planId} AND es.ExamPlanInfoID = #{planId}")
    void delExamScenesByScenesIds(@Param(value = "appID") String appID, @Param(value = "planId") String planId);

    @Select("SELECT * FROM ExamScenes WHERE ExamPlanInfoID = #{examPlanID} LIMIT 1")
    ExamScenes getExamSceneByExamPlanID(@Param(value = "examPlanID") String examPlanID);

    @Select("SELECT ExamPlanInfoID,ExamSceneID FROM examscenes WHERE PlanEndTime < NOW()")
    List<ExamScenes> findEndScenesInfo();
}
